<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Cat extends Model
{
    //连接数据表
    protected $table = 'cat';

    //指定填充字段
    protected $fillable = [

    ];

    //是否加时间戳
    public $timestamps = false;


    /*栏目原始数据*/
    //找子级所有栏目
    public function findTree($data,$id=0,$lev=0)
    {
    	static $tree=[];
    	foreach($data as $v){
    		if($v->parentid==$id){
    			$v->lev=$lev;
    			$tree[]=$v;
    			$this->findTree($data,$v->catid,$lev+1);
    		}
    	}
    	return $tree;
    }


    //查数据库
    public function findAll()
    {
    	$data = self::select('catid','parentid','catname')->get();
    	return $this->findTree($data,0,0);
    }

    //找下一级目录
    public function findSon($data,$id=0)
    {
    	$sons=[];
    	foreach($data as $v){
    		if($v->parentid==$id){
    			$sons[]=$v;
    		}
    	}
    	return $sons;
    }
}
